package com.origon360.Services;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.origon.entities.Language;
import com.origon.entities.Role;
import com.origon.entities.User;
import com.origon360.security.SHA256CredentialManagerImpl;


@Stateless
public class RegistrationService {
	
	@PersistenceContext
	EntityManager em;
	
	@EJB
	private UserService userService;
	
	private List<Role> roleList = new ArrayList<Role>();
	
	public void createUser(User user){
		String encryptedPassword = SHA256CredentialManagerImpl.getSHA256EncryptionWithSalt(user.getPassword());
		
		user.setPassword(encryptedPassword);
		
		roleList.add(userService.getUserRole());
		user.setRoles(roleList);
		
		//user.getRoles().add(userService.getUserRole());
		user.setActivationKey(UUID.randomUUID().toString());
		em.persist(user);
	}
	
	
	public List<User> fetchUsers(){
		return em.createQuery("FROM User", User.class).getResultList();
	}
	
	public List<Language> fetchLaguages(){
		return em.createQuery("FROM Language", Language.class).getResultList();
	}

}
